﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GrayParrot.Core;
using Test.DAL;


namespace Dashboard.BL.Facades
{
    public class EmployeeFacade : FacadeBase<EmployeeFacade>
    {
        NorthWindContext m_ctx = null;

        public Repository<Employee, string> Repo { get; internal set; }

        public EmployeeFacade()
        {
        }

        public override void Initialize()
        {
            m_ctx = new NorthWindContext();
            m_ctx.Configuration.LazyLoadingEnabled = false;
            m_ctx.Configuration.ProxyCreationEnabled = false;

            Repo = this.GetRepository<Employee, string>(m_ctx);
        }

        public override void Dispose()
        {
            if (m_ctx != null)
            {
                m_ctx.Dispose();

                Repo.Dispose();
            }
        }

        public ReturnValue<bool> Add(Employee item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                if (item.EmployeeId == default(int))
                {
                    item.EmployeeId = 1;
                }

                this.Repo.Add(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Edit(Employee item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Edit(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<bool> Delete(Employee item)
        {
            ReturnValue<bool> retValue = new ReturnValue<bool>();

            try
            {
                this.Repo.Delete(item);
                this.Repo.Save();

                retValue.HasValue = true;
                retValue.Value = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<Employee> Get(string id)
        {
            ReturnValue<Employee> retValue = new ReturnValue<Employee>();

            try
            {
                retValue.Value = this.Repo.Get(id);
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        public ReturnValue<IQueryable<Employee>> GetAll()
        {
            ReturnValue<IQueryable<Employee>> retValue = new ReturnValue<IQueryable<Employee>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                retValue.Value = this.Repo.GetAll();
                retValue.HasValue = true;
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }

        //public ReturnValue<List<string>> GetCompanies()
        //{
        //    ReturnValue<List<string>> retValue = new ReturnValue<List<string>>();
        //    retValue.HasValue = false;
        //    retValue.Value = null;

        //    try
        //    {
        //        var objs = (from x in m_ctx.Customers
        //                    select x.CompanyName).Distinct();

        //        if (objs.Any())
        //        {
        //            retValue.Value = objs.ToList();
        //            retValue.HasValue = true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        retValue.HasError = true;
        //        retValue.Error = ex;
        //    }

        //    return retValue;
        //}
    }
}
